var zTreeObj;
var menus = $("#menus").val();
// zTree 的参数配置，深入使用请参考 API 文档（setting 配置详解）
var setting = {
		check: {
			enable: true ,
			chkStyle: "checkbox",
			chkboxType: { "Y": "s", "N": "s" }
		},
		data: {
			simpleData: {
				enable: true
			}
		},
		callback: {
	        onClick: function(event,treeId,treeNode) {
	        	$("#testIframe").attr("src",treeNode.fileUrl);
	        }
	    }
};
// zTree 的数据属性，深入使用请参考 API 文档（zTreeNode 节点数据详解）
//;console.log(menus);
$(document).ready(function(){
   var result = jQuery.parseJSON(menus);
   var zNodes = [];
   for (var i = 0; i < result.length; i++) {
       zNodes.push({
           id: result[i].id,
           pId: result[i].pId,
           name: result[i].name,
           fileUrl:result[i].fileUrl,
           menuType:result[i].menuType,
           checked:result[i].checked,
           hasChildren: result[i].hasChildren
       });
   }
   $.fn.zTree.init($("#treeDemo"), setting, zNodes);
   var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
   treeObj.expandAll(true);
   $("#treeDemo_1_span").click();
   
});

function reinitIframe(){//iframe自适应高度
	var iframe = document.getElementById("testIframe");
	try{
		var bHeight = iframe.contentWindow.document.body.scrollHeight;
		var dHeight = iframe.contentWindow.document.documentElement.scrollHeight;
		var height = Math.max(bHeight, dHeight);
		iframe.height = height;
	}catch (ex){}
}
window.setInterval("reinitIframe()", 200);


function submit(){
	 var treeObj=$.fn.zTree.getZTreeObj("treeDemo"),
     nodes=treeObj.getCheckedNodes(true),
     menuIds="";
     for(var i=0;i<nodes.length;i++){
    	 menuIds+=nodes[i].id + ",";
     }
     $('#menuIds').val(menuIds);
	$("#saveAndUpdateFrom").attr("action", JSSYSMANAGERV1 + '/role/updateAuthority');
	$("#saveAndUpdateFrom").submit();
}